﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ttifa.Infrastructure.Extensions
{
    public static class StringExtensions
    {
        #region 填充字符
        /// <summary>
        /// 左侧补充（空格）字符串到指定字节长度
        /// </summary>
        /// <param name="text">原字符串</param>
        /// <param name="size">规定字节长度</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string BytePadLeft(this string text, int size, string encoding = "GBK")
        {
            var length = text.Length;
            var byteCount = Encoding.GetEncoding(encoding).GetByteCount(text);
            if (byteCount < size)
                text = text.PadLeft(length + size - byteCount);

            return text;
        }

        /// <summary>
        /// 左侧补充字符串到指定字节长度长度
        /// </summary>
        /// <param name="text">原字符串</param>
        /// <param name="size">规定字节长度</param>
        /// <param name="c">填充字符</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string BytePadLeft(this string text, int size, char c, string encoding = "GBK")
        {
            var length = text.Length;
            var byteCount = Encoding.GetEncoding(encoding).GetByteCount(text);
            if (byteCount < size)
                text = text.PadLeft(length + size - byteCount, c);

            return text;
        }

        /// <summary>
        /// 右侧补充（空格）字符串到指定字节长度
        /// </summary>
        /// <param name="text">原字符串</param>
        /// <param name="size">规定字节长度</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string BytePadRight(this string text, int size, string encoding = "GBK")
        {
            var length = text.Length;
            var byteCount = Encoding.GetEncoding(encoding).GetByteCount(text);
            if (byteCount < size)
                text = text.PadRight(length + size - byteCount);

            return text;
        }

        /// <summary>
        /// 右侧补充字符串到指定字节长度
        /// </summary>
        /// <param name="text">原字符串</param>
        /// <param name="size">规定字节长度</param>
        /// <param name="c">填充字符</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string BytePadRight(this string text, int size, char c, string encoding = "GBK")
        {
            var length = text.Length;
            var byteCount = Encoding.GetEncoding(encoding).GetByteCount(text);
            if (byteCount < size)
                text = text.PadRight(length + size - byteCount, c);

            return text;
        }
        #endregion

        #region 按字节长度截取
        /// <summary>
        /// 获取字符串字节长度
        /// </summary>
        /// <param name="text"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static int ByteCount(this string text, string encoding = "GBK")
        {
            return Encoding.GetEncoding(encoding).GetByteCount(text);
        }

        public static string ByteSubstring(this string text, int start, string encoding = "GBK")
        {
            var _encoding = Encoding.GetEncoding(encoding);
            byte[] bytes = _encoding.GetBytes(text);
            return _encoding.GetString(bytes, start, bytes.Length - start);
        }

        public static string ByteSubstring(this string text, int start, int length, string encoding = "GBK")
        {
            var _encoding = Encoding.GetEncoding(encoding);
            byte[] bytes = _encoding.GetBytes(text);
            return _encoding.GetString(bytes, start, length);
        }
        #endregion

        /*
        /// <summary>
        /// 获取指定字节长度的中英文混合字符串
        /// </summary>
        public static string GetString(this string text, int start, int length, string encoding = "GBK")
        {
            string result = string.Empty;
            int byteLen = System.Text.Encoding.Default.GetByteCount(text);// 字节长度
            int charLen = text.Length;// 把字符长度
            int byteCount = 0;// 记录读取进度
            int pos = 0;// 记录截取位置
            if (byteLen > length)
            {
                for (int i = 0; i < charLen; i++)
                {
                    if (Convert.ToInt32(text.ToCharArray()[i]) > 255)// 按中文字符计算加2
                        byteCount += 2;
                    else
                        byteCount += 1;
                    if (byteCount > length)// 超出时只记下上一个有效位置
                    {
                        pos = i;
                        break;
                    }
                    else if (byteCount == length)// 记下当前位置
                    {
                        pos = i + 1;
                        break;
                    }
                }

                if (pos >= 0)
                    result = text.Substring(start, pos);
            }
            else
                result = text;

            return result;
        }
        */
    }
}
